package team.scau.laboratory_repair.common.json;

import com.querydsl.core.QueryResults;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

/**
 * @author 30254
 * creadtedate:2018/5/18
 */

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class JqGridResult implements Serializable {
    private Long page;
    private Long total;
    private Long records;
    private List<?> list;
    private Object userData;

    public JqGridResult(Long page, Long total, Long records, List<?> list) {
        this(page, total, records, list, null);
    }

    public JqGridResult(QueryResults<?> results, List<?> list){
        setRecords(results.getTotal());
        setList(list);
        setPage(results.getOffset());
        setTotal(records % results.getLimit() > 0 ? records / results.getLimit() + 1 : records / results.getLimit());
    }

    public JqGridResult(List<?> list){
        this(1L, 1L, (long) list.size(), list);
    }
    public JqGridResult(QueryResults<?> results) {
        setRecords(results.getTotal());
        setList(results.getResults());
        setPage(results.getOffset());
        setTotal(records % results.getLimit() > 0 ? records / results.getLimit() + 1 : records / results.getLimit());
    }

}
